<script setup lang="ts">
import { ref } from 'vue'
// 导入小程序生命周期钩子
import { onLoad } from '@dcloudio/uni-app'
import { getHomeRecommend } from '@/services/home'
import type { GoodsItem, SubType } from '@/types/home'

// ------------------ 自己写的代码 ----------------------------
const { type } = defineProps<{
  type: string
}>()
// 广告图
const bannerPicture = ref('')
// Tabs 选项卡
type SubTypesAndGoods = SubType & GoodsItem // TS 交叉类型(把两个类型合在一起)
const subTypes = ref<SubTypesAndGoods[]>([])
// 统一管理推荐请求
const urlMap = [
  { type: '1', title: '特惠推荐', url: '/home/preference/mutli' },
  { type: '2', title: '爆款推荐', url: '/home/inVogue/mutli' },
  { type: '3', title: '一站买全', url: '/home/oneStop/mutli' },
  { type: '4', title: '领券中心', url: '/home/new/mutli' }, // 后端未提供，暂用新鲜好物url
  { type: '5', title: '新鲜好物', url: '/home/new/mutli' },
]
let currUrl = ''
// 小程序页面加载生命周期钩子，可以获取页面参数
onLoad(async () => {
  // 根据页面参数的 type 查找当前是什么推荐
  const currUrlMap = urlMap.find((v) => v.type === type)
  // 如果找到了
  if (currUrlMap) {
    // 保存请求 url
    currUrl = currUrlMap.url
    // 🎯调用通用的推荐接口，动态传递当前匹配的 url
    const res = await getHomeRecommend(currUrlMap.url, {})
    console.log(res)
    // 动态更新导航栏标题
    uni.setNavigationBarTitle({ title: res.title })
    // 保存轮播图
    bannerPicture.value = res.bannerPicture
    // 保存 Tabs 数据
    // subTypes.value = res.subTypes
    // 把 Tabs 数据和 列表数据 关联起来
    res.subTypes.forEach((item) => {
      const obj = {
        ...item, // Tabs 数据
        ...res.goodsItems[Number(item.id)], // 对应的列表数据
      }
      // 追加到数组中
      subTypes.value.push(obj)
    })
    console.log(subTypes.value)
  }
})

// 高亮的下标
const activeIndex = ref(0)
const changeTabs = (index: number) => {
  activeIndex.value = index
}

// 滚动到底部
const onScrolltolower = async () => {
  // console.log('滚动到底部')
  // 获取当前的 SubTypes
  const currSubTypes = subTypes.value[activeIndex.value]
  // 判断是否已经加载完毕
  if (currSubTypes.counts === currSubTypes.items.length) {
    // 退出并提示用户
    return uni.showToast({ icon: 'none', title: '没有更多啦~' })
  }
  // 当前页码+1
  currSubTypes.page++
  // 发送请求获取当前 SubTypes 的分页数据
  const res = await getHomeRecommend(currUrl, {
    pageSize: 10,
    page: currSubTypes.page,
    subType: currSubTypes.id,
  })
  // console.log(116, res.goodsItems[Number(currSubTypes.id)].items)
  // 追加到当前选项卡的列表中
  const items = res.goodsItems[Number(currSubTypes.id)].items
  currSubTypes.items.push(...items)
}
</script>

<template>
  <view class="viewport">
    <!-- 推荐封面图 -->
    <view class="cover">
      <image :src="bannerPicture"></image>
    </view>
    <view class="tabs">
      <text
        class="text"
        v-for="(item, index) in subTypes"
        :key="item.id"
        :class="{ active: index === activeIndex }"
        @tap="changeTabs(index)"
      >
        {{ item.title }}
      </text>
    </view>
    <scroll-view
      v-for="(item, index) in subTypes"
      :key="item.id"
      scroll-y
      enhanced
      :show-scrollbar="false"
      class="scroll-view"
      v-show="activeIndex === index"
      @scrolltolower="onScrolltolower"
    >
      <view class="goods">
        <navigator
          hover-class="none"
          class="navigator"
          v-for="goods in item.items"
          :key="goods.id"
          :url="`/pages/goods/index?id=${goods.id}`"
        >
          <image class="thumb" :src="goods.picture"></image>
          <view class="name ellipsis">{{ goods.name }}</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">{{ goods.price }}</text>
          </view>
        </navigator>
      </view>
      <view class="loading">正在加载...</view>
    </scroll-view>
  </view>
</template>

<style>
page {
  height: 100%;
  background-color: #f4f4f4;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}

.cover {
  width: 750rpx;
  height: 225rpx;
  border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;

  position: absolute;
  left: 0;
  top: 0;
}

.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  margin: 0 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;
}

.tabs .text {
  margin: 0 20rpx;
  position: relative;
}

.tabs .active::after {
  content: '';
  width: 40rpx;
  height: 4rpx;
  transform: translate(-50%);
  background-color: #27ba9b;
  position: absolute;
  left: 50%;
  bottom: 24rpx;
}

.scroll-view {
  flex: 1;
}

.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;
}

.goods .navigator {
  width: 345rpx;
  padding: 20rpx;
  margin-top: 20rpx;
  border-radius: 10rpx;
  background-color: #fff;
}

.goods .thumb {
  width: 305rpx;
  height: 305rpx;
}

.goods .name {
  height: 88rpx;
  font-size: 26rpx;
}

.goods .price {
  line-height: 1;
  color: #cf4444;
  font-size: 30rpx;
}

.goods .symbol,
.goods .decimal {
  font-size: 70%;
}
</style>
